/*
 *  AMPStoriesWidgetMgr.cpp
 *  AMPlugin
 *
 *  Created by Vu Tiet on 8/10/12.
 *  Copyright 2012 __MyCompanyName__. All rights reserved.
 *
 */


#include "VCPlugInHeaders.h"

#include "IControlView.h"
#include "IInLineEditController.h"
#include "IPanelControlData.h"
#include "ITextControlData.h"
#include "ITreeViewController.h"

#include "CTreeViewWidgetMgr.h"
#include "AMPStoryNodeID.h"
#include "AMPID.h"
#include "AMGlobal.h"
#include "AMPLogDataRecord.h"
#include <iostream>
using namespace std;

#include "K2Vector.h"
#include "K2Vector.tpp"


/**
 *  Tree view manager
 */
class AMPStoriesWidgetMgr : public CTreeViewWidgetMgr
	{
	public:
		AMPStoriesWidgetMgr(IPMUnknown *boss);
		virtual ~AMPStoriesWidgetMgr()
		{}
		
		virtual bool16	ApplyDataToWidget( const NodeID& node, IPanelControlData* widgetList, int32 message ) const;
		
		void RegisterStyles()
		{
			RegisterStyleWidget(kLargePaletteRowsTreeStyle, kStoryNodeWidgetRsrcID, kAMPPluginID);
		}
		
		void setIconForContent(IControlView* widget, const PMString& path) const;

	};


CREATE_PMINTERFACE(AMPStoriesWidgetMgr, kAMPStoriesWidgetMgrImpl)

AMPStoriesWidgetMgr::AMPStoriesWidgetMgr(IPMUnknown *boss) :
CTreeViewWidgetMgr(boss, kList)
{
	do {
		/*
		InterfacePtr<ITreeViewController> 	controller(this, UseDefaultIID());
		ASSERT(controller);
		if(!controller)
		{
			//cout << "cannot get ITreeViewController" << endl;
			break;
		}
		
		PMString item;
		NodeIDList selectedItems;
		controller->GetSelectedItems(selectedItems);
		
		const int kSelectionLength =  selectedItems.size() ;
		if (kSelectionLength> 0 )
		{
			K2Vector<NodeID>::const_iterator iter, startIter, endIter;
			startIter = selectedItems.begin();
			endIter = selectedItems.end();
			for(iter = startIter; iter != endIter; ++iter)
			{
				const AMPStoryNodeID* oneNode = static_cast<const AMPStoryNodeID*>(iter->Get());
				item = oneNode->GetName();
				//item = oneNode->GetId();
				item.Translate();
			}
			//cout << "----------selectedItem: " << item.GrabCString() << endl;
		}
		 */
	}while(kFalse);
	
}



bool16 AMPStoriesWidgetMgr::ApplyDataToWidget(const NodeID& node, 
										 IPanelControlData* widgetList, 
										 int32 message) const
{
	do
	{
		//cout << "AMPStoriesWidgetMgr::ApplyDataToWidget" << endl;
		InterfacePtr<ITreeViewController> 	controller(this, UseDefaultIID());
		ASSERT(controller);
		if(!controller)
		{
			//cout << "cannot get ITreeViewController" << endl;
			break;
		}
		
		TreeNodePtr<AMPStoryNodeID> nodeID(node);
		PMString listName = nodeID->GetName();
		PMString listID = nodeID->GetId();
		//cout << "++++listName: " << listName.GrabCString() << endl;

		IControlView* nameView = widgetList->FindWidget(kAMPStoryNodeNameWidgetID);
		
		InterfacePtr<ITextControlData>	textControlData( nameView, UseDefaultIID() );
		ASSERT(textControlData);
		if( textControlData== nil) {
			break;
		}
		textControlData->SetString(listName);
		
		IControlView* iconView = 
		widgetList->FindWidget(kCheckIconWidgetID);
		ASSERT(iconView);
		if (iconView == nil) {
			break;
		}
		
		bool shouldDisplay = FALSE;
		
		if (g_ChosenStoryID.length() > 0) {
			if (g_ChosenStoryID == listID.GrabCString())
				shouldDisplay = TRUE;
		}
		else {
			for (int i = 0; i < g_ImageInfo.size(); i++) {
				images_t iImgInfo = g_ImageInfo[i];
				////cout << "iImgInfo.story_id & listID: " << iImgInfo.story_id << " & " << listID.GrabCString() << endl;
				if (iImgInfo.image_path == g_ChosenImagePath && iImgInfo.story_id == listID.GrabCString()) {
					shouldDisplay = TRUE;
					break;
				}
			}
		}
		
		
		
		if (shouldDisplay == TRUE) {
			iconView->Show(kTrue);
			controller->Select(node, kTrue, kTrue);
			//cout << "select node: " << listName.GrabCString() << endl;
		}
		else {
			iconView->Hide();
			controller->Deselect(node, kTrue, kFalse);
			//cout << "deselect node: " << listName.GrabCString() << endl;
		}
		
		
	} while (false);	
	return kTrue;
}

